//
//  StrategyHeadView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/28.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol StrategyHeadViewDelegate {
    @objc optional func selectStrategyLabel(index:Int) //选择攻略标签
}

class StrategyHeadView: UIView {
    
    weak var delegate : StrategyHeadViewDelegate?
    
    fileprivate let cellId = "strategyCell"
    fileprivate let dataArray = [creditCard,creditCard+service_title,cardSchool,amountRaise,popularWool,cardOffer]
    
    fileprivate var collectionView : UICollectionView!

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        let layout = LeftEqualFlowLayout()
        layout.minimumLineSpacing = RATIO_H(maxNum: 10)
        
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.isScrollEnabled = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TypeCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 15))
            make.bottom.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension StrategyHeadView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? TypeCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        cell?.titleLabel.backgroundColor = UIColor.mineColor()
        cell?.titleLabel.layer.cornerRadius = RATIO_H(maxNum: 15)
        cell?.titleLabel.layer.masksToBounds = true
        cell?.titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let width = dataArray[indexPath.row].size(font: RATIO(maxNum: 16), height: RATIO_H(maxNum: 30)).width + 20
        
        return CGSize.init(width: width, height: RATIO_H(maxNum: 30))
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        self.delegate?.selectStrategyLabel!(index: indexPath.row)
    }
    
}
